package marker.micro.login.entity;

import javax.persistence.Entity;
import javax.persistence.Table;

import org.springframework.security.crypto.bcrypt.BCrypt;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import marker.micro.common.BaseID;

@Entity
@Table(name="users")
@NoArgsConstructor(access=AccessLevel.PRIVATE) //私有化构造函数，不允许外部通过new来实例化
@Getter
@EqualsAndHashCode(callSuper = false)
public class User extends BaseID{

    /**
     * 
     */
    private static final long serialVersionUID = 484700361475113289L;

    private String username;

    private String password;

    /**
     * 角色
     */
    private RoleType role;

    private void setRole(RoleType role) {
	this.role = role;
    }

    private Boolean enabled;

    public static User create(
	    String username,
	    String password,
	    RoleType role){
	User user = new User();
	user.setUsername(username);
	user.initOrUpdatePassword(password);
	user.setRole(role);
	user.setEnabled(true);
	// 注册领域事件
	//	user.registerEvent(new UserCreatedEvent(user));
	return user;
    }

    public String getUsername() {
	return username;
    }

    public void setUsername(String username) {
	this.username = username;
    }

    public Boolean getEnabled() {
	return enabled;
    }

    public void setEnabled(Boolean enabled) {
	this.enabled = enabled;
    }

    public void initOrUpdatePassword(String password){
	this.password = BCrypt.hashpw(password, BCrypt.gensalt());
    }
    

    public String getPassword() {
        return password;
    }



    /**
     * 角色枚举
     */
    public enum RoleType {
	ROLE_ADMIN, // 超级管理员
	ROLE_MANAGER, // 主管
	ROLE_USER; // 用户
    }

}
